package com.imooc.controller;

import com.imooc.eo.Stu;
import com.imooc.result.GraceJSONResult;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liansen
 * @create 06-14-11:46
 */
@RestController
@RequestMapping("/es")
public class Hello {

    @Autowired
    private ElasticsearchRestTemplate esTemplate;

    //创建索引
    @GetMapping("/createIndex")
    public Object createIndex(){
        //创建Stu索引
        esTemplate.indexOps(Stu.class).create();
        return GraceJSONResult.ok();
    }

    //删除索引
    @GetMapping("/deleteIndex")
    public Object deleteIndex(){
        esTemplate.indexOps(Stu.class).delete();
        return GraceJSONResult.ok();
    }


    //保存文档数据
    @GetMapping("/save")
    public Object save(){
        Stu stu = new Stu();
        stu.setId(1001L);
        stu.setName("陈平安");
        stu.setAge(27);
        stu.setMoney(66.6f);
        stu.setDescription("平平安安");

        IndexQuery query = new IndexQueryBuilder().withObject(stu).build();
        IndexCoordinates ic = IndexCoordinates.of("stu");

        String index = esTemplate.index(query, ic);
        System.out.println("index:" + index);
        return GraceJSONResult.ok();
    }

    //修改文档数据
    @GetMapping("/update")
    public Object update(){

        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "慕课网");
        map.put("age", 6);
        map.put("money", 99.9f);
        map.put("description", "课程凑合");

        UpdateQuery updateQuery = UpdateQuery.builder("1001").withDocument(Document.from(map)).build();

        IndexCoordinates ic = IndexCoordinates.of("stu");
        esTemplate.update(updateQuery, ic);
        return GraceJSONResult.ok();
    }

    //查询文档数据 (按条件查询一条数据)
    @GetMapping("/get")
    public GraceJSONResult get(){

        Criteria criteria = new Criteria().and("Id").is(1001);
        Query query = new CriteriaQuery(criteria);
        Stu stu = esTemplate.searchOne(query, Stu.class).getContent();
        return GraceJSONResult.ok(stu);
    }

    //按条件查询多条数据
    @GetMapping("/getList")
    public GraceJSONResult getList(){

        Criteria criteria = new Criteria("age").is(6);
        Query query = new CriteriaQuery(criteria);
        SearchHits<Stu> search = esTemplate.search(query, Stu.class, IndexCoordinates.of("stu"));
        List<SearchHit<Stu>> searchHits = search.getSearchHits();
        List<Stu> stuList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
        return GraceJSONResult.ok(stuList);
    }

    @GetMapping("/match_all")
    public GraceJSONResult matchAll(Integer page, Integer pageSize){
        Pageable pageable = PageRequest.of(page, pageSize);
        Query query = new NativeSearchQueryBuilder()
                                        .withQuery(QueryBuilders.matchAllQuery())
                                        .withPageable(pageable)
                                        .build();
        SearchHits<Stu> hits = esTemplate.search(query, Stu.class);
        List<SearchHit<Stu>> searchHits = hits.getSearchHits();
        List<Stu> collect = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
        return GraceJSONResult.ok(collect);
    }

}

